/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pardal.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import weka.core.Instances;
import weka.core.Utils;

/**
 *
 * @author Valdigleis
 */
public class ManipulationHMC extends ArffManipulationBase{

    public ManipulationHMC(Instances instances) {
        super(instances);
    }
    
    public ArrayList<Node> getNodesHMC() {
        
        ArrayList<Node> nodes = super.getNodes();
        ArrayList<Node> nodesReturn = new ArrayList<>();
        
        Collections.sort(nodes,new Comparator(){
            public int compare(Object node1, Object node2) {
                Node alfa = (Node) node1;
                Node beta = (Node) node2;
                return alfa.getLevel() < beta.getLevel() ? -1: alfa.getLevel() > beta.getLevel() ? +1 : 0;
            }
        });
        
        for (int i = 0; i < nodes.size(); i++) {
            if(nodes.get(i).getSons() == null || nodes.get(i).getSons().isEmpty()){
            }else{
                nodesReturn.add(nodes.get(i));
            }
        }
        return nodesReturn;
    }
    
    
    public Instances buildInstancesForNode(Node node){
        //Lista para os labels do nivel
        ArrayList<String> labels = new ArrayList<>();
        //Instâncias de retorno
        Instances dataNodeHMC = new Instances(getInstances());
        //Pega os labels dos filhos
        String son;
        for (int i = 0; i < node.getSons().size(); i++) {
            son = node.getSons().get(i);
            labels.add(son);
        }
        String classe;
        boolean test;
        for (int i = 0; i < dataNodeHMC.numInstances(); i++) {
            classe = dataNodeHMC.instance(i).stringValue(dataNodeHMC.classIndex());
            test = true;
            for (int j = 0; j < labels.size(); j++) {
                if(classe.contains(labels.get(j)) || classe.equals(labels.get(j))){
                    dataNodeHMC.instance(i).setValue(dataNodeHMC.classIndex(), labels.get(j));
                    test = false;
                    break;
                }
            }
            if(test){
                dataNodeHMC.instance(i).setValue(dataNodeHMC.classIndex(), Utils.missingValue());
            }
        }
        dataNodeHMC.deleteWithMissingClass();
        System.out.println();
        return dataNodeHMC;
    }
    
}
